package com.ssyan.boot;

import com.ssyan.boot.bean.Pet;
import com.ssyan.boot.bean.User;
import com.ssyan.boot.bean.UserImport;
import com.ssyan.boot.config.MyConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

/*
    主程序类
    @SpringBootApplication:声明这是一个SpringBoot应用
 */
//注解属性：scanBasePackages：指定包扫描路径范围
//注解：@ComponentScan指定包扫描路径
/*
* @SpringBootApplication是一个合成注解，可以分解为多个子注解进行详细配置
* @SpringBootApplication = @SpringBootConfiguration + @EnableAutoConfiguration + @ComponentScan("")
* */
@SpringBootApplication //(scanBasePackages = "com.ssyan")
//@ComponentScan("com.ssyan")
public class MainApplication {

    public static void main(String[] args) {
        //1.返回ioc容器的组件
        ConfigurableApplicationContext run = SpringApplication.run(MainApplication.class, args);

        //2.查看容器里面的组件
        String[] beanDefinitionNames = run.getBeanDefinitionNames();
        for (String beanDefinitionName : beanDefinitionNames) {
            System.out.println(beanDefinitionName);
        }

        //3.从容器中获取组件
        Pet tom01 = run.getBean("tom", Pet.class);
        Pet tom02 = run.getBean("tom", Pet.class);
        System.out.println("组件单实例？："+(tom01 == tom02) );

        //配置类本身也是一个组件，而且默认是一个代理对象
        MyConfig bean = run.getBean(MyConfig.class);
        System.out.println(bean);//com.ssyan.boot.config.MyConfig$$EnhancerBySpringCGLIB$$5d5bc945@55f45b92

        //组件依赖 -- Lite模式非单实例，Full模式单实例
        User user01 = run.getBean("user01", User.class);
        Pet tom = run.getBean("tom", Pet.class);

        System.out.println("用户宠物与容器宠物是否同一只？："+(user01.getPet()==tom));

        //4.测试@Import
        UserImport im = run.getBean(UserImport.class);
        im.setName("lisi");
        im.setAge(10);
        System.out.println("import="+im);

        //获取多组件
        String[] beanNamesForType = run.getBeanNamesForType(User.class);
        System.out.println("=======");
        for (String s : beanNamesForType) {
            System.out.println(s);
        }

        //5.测试@Conditional
        boolean usertom = run.containsBean("usertom");
        System.out.println("容器中是否含有自定义Pet组件："+usertom);
        boolean userxx = run.containsBean("user01");
        System.out.println("容器中是否有组件依赖Pet的User组件："+userxx);

        //6.测试@ImportResource
        boolean haha = run.containsBean("haha");
        boolean hehe = run.containsBean("hehe");
        System.out.println("容器中是否有配置文件中的haha组件:"+haha);
        System.out.println("容器中是否有配置文件中的hehe组件:"+hehe);

    }
}
